package com.foreknow.springboot_demo.service.Impl;

import com.foreknow.springboot_demo.mapper.CartMapper;
import com.foreknow.springboot_demo.mapper.OrderDetailetMapper;
import com.foreknow.springboot_demo.mapper.OrdersMapper;
import com.foreknow.springboot_demo.po.Cart;
import com.foreknow.springboot_demo.po.OrderDetailet;
import com.foreknow.springboot_demo.po.Orders;
import com.foreknow.springboot_demo.service.OrderService;
import com.foreknow.springboot_demo.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderDetailetMapper orderDetailetMapper;


    @Override
    public Integer createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {

        //1.创建订单,获取订单编号
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        orders.setOrderTotal(orderTotal);
        orders.setOrderDate(CommonUtil.getCurrentDate());
        //创建订单返回主键
        ordersMapper.saveOrders(orders);
        Integer orderId = orders.getOrderId();
        System.out.println("订单编号"+orderId);
        //2.查询购物车内的信息
        List<Cart> listCart = cartMapper.listCart(userId,businessId);
        //3.批量插入到订单明细
        orderDetailetMapper.saveToOrdersDetailet(orderId,listCart);
        //4.清空购物车内的信息
        cartMapper.removeCart(userId,null,businessId);
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        return ordersMapper.getOrdersById(orderId);
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        return ordersMapper.listOrdersByUserId(userId);
    }
}
